<?php session_start(); 
	include('user_type/system.php');
	include('access/login_functions.php');
	require("access/connect.php");
	$_SESSION['restricted'] = false;
	
	//check for stopwords
	if (isset($_POST['search'])){
		$search = $_POST['search'];
		
		$res = mysql_query("SELECT Stopword FROM stopword_table WHERE Stopword like '%$search%'")or die("Stopword could not be retrieved. ".mysql_error());
		$arr = mysql_fetch_array($res);
		if(!empty($arr)){
			$_SESSION['captchaWarning'] =  "<center>Your search query was denied because it contains stopwords </center><br>";
			send_to_page("home.php");	//stopword found
			}
		}
		
	// search queries as temporary session variables
	if (isset($_POST['search']))
		$_SESSION['search'] = $_POST['search'];
	
	if (isset($_POST['searchType']))
		$_SESSION['searchType'] = $_POST['searchType'];
		
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
		<meta name="keywords" content="KEYWORDS HERE!!!!">
		<meta name="description" content="Descrition Here!!!">
		<title>Capcha Page</title>
	<link rel="stylesheet" type="text/css" href="style_new.css">
</head>
<body>


<div class="above_body"> <!-- closing tag is in template navbar -->
<div id="header" class="floatcontainer doc_header">
	<div><a name="top" href="home.php" class="logo-image"><img src="images\QUESTwtrl2.png"  title=""></a></div>
	<div id="toplinks" class="toplinks">
		
			<ul class="nouser">
			
				<?php if (!isset($_SESSION['user'])) echo "<li><a href=\"register.php\" rel=\"nofollow\">Register</a></li>"; ?>
			
				<li>
			
				<form id="navbar_loginform" action="access/login.php" method="post" >
					<input type="hidden" name="currentPage" value="<?php echo System::curPageName(); ?>" />
						<fieldset id="logindetails" class="logindetails">
							<div>
								<div>
							
							<?php // check if user is logged in
							if(isset($_SESSION['user']))
							{	$user = $_SESSION['user'];
								$_SESSION['curPage'] = System::curPageName(); 
								// if so, display username with tools
								echo "<div style=\"font-size: 16px; font-weight: bold;\">";
								echo "Welcome, $user <br />";
								echo "<a href=\"access/logout.php\">Log Out</a>";
								echo "<a href=\"userTools.php\">myTools</a>";
								echo "<a href=\"userInfo.php\">myProfile</a>";
								
								echo "</div>";
							}
							else
							{	// otherwise, display login form
								echo "<input type=\"text\" class=\"textbox default-value\" name=\"user\" id=\"navbar_username\" size=\"10\" tabindex=\"101\" value=\"Username\"> ";
								echo "<input type=\"password\" class=\"textbox default-value\" tabindex=\"102\" name=\"pass\" id=\"navbar_password_hint\" size=\"10\" value=\"Password\" style=\"display: inline; \"> ";
								echo "<input type=\"submit\" class=\"loginbutton\" tabindex=\"104\" value=\"Log in\" title=\"Enter your username and password in the boxes provided to login.\" accesskey=\"s\">";
							}
						?>
				</form>
				</li>
			</ul>
		
	</div>
	<div class="ad_global_header">
		 
		 
	</div>
	<hr>
</div> 
<div id="navbar" class="navbar">

	<ul id="navtabs" class="navtabs floatcontainer">					
	</ul>
	
</div>
</div><!-- closing div for above_body -->

<?php

	$image_name = array();
	$image_counter_query = "SELECT COUNT(*) FROM images_table";
	
	//count number of images
	if(!($result = mysql_query($image_counter_query)))
		{
		echo 'Image Counter Query failed. '.mysql_error();
		exit();
		}
     else
		$result = mysql_fetch_row($result);
		$image_counter = intval($result[0]);
				
		
	$image_name_query1 = "SELECT tb.filename, tb.CID FROM 
				(SELECT filename, CID FROM images_table WHERE KW1 IS NULL)tb
				ORDER by FLOOR(1 + (RAND() * $image_counter)) LIMIT 1";
	
	$image_name_query2 = "SELECT tb.filename, tb.CID FROM 
				(SELECT filename, CID FROM images_table WHERE KW1 IS NOT NULL)tb
				ORDER by FLOOR(1 + (RAND() * $image_counter)) LIMIT 1";
	
	//get unknown images
	
	$name_result1 = mysql_query($image_name_query1)or die('Image Name Query failed. '.mysql_error());
	$name_result1 = mysql_fetch_row($name_result1);
	$image_name[0] = $name_result1[0];
	$image_ID[0]= intval($name_result1[1]);
	
	//get known images
	$name_result2 = mysql_query($image_name_query2)or die('Image Name Query failed. '.mysql_error());
	$name_result2 = mysql_fetch_row($name_result2);
	$image_name[1] = $name_result2[0];
	$image_ID[1]= intval($name_result2[1]);
		
	$rnd_number = rand(0,1);
	
	//echo $rnd_number;
	
	$image1 = array($image_name[$rnd_number], $image_ID[$rnd_number]);
	$image2 = array($image_name[pow(($rnd_number-1),2)], $image_ID[pow(($rnd_number-1),2)]);
	
	@mysql_connect(close);
?>

<div class="article_block">

	<h2 style="font-size:14px; font-weight:Bold" class="blockhead">Please write the corressponding keyword for each image to proceed with the search</h2>
	<form class="block" action="captcha_validate.php" method="post" name="captcha">
		<div class="blockbody formcontrols">
			<div class="section">
				<div class="blockrow">
					<table>
						<tr><td></td><td></td><td></td><td></td></tr>
						<tr>
							<td style="width:10%"></td>
							<td><img src="<?php echo $image1[0]?>"/></td>
							<td style="width:20%"></td>
							<td style="width:20%"></td>
							<td><img src="<?php echo $image2[0]?>"/></td>
							<td style="width:10%"></td>
						</tr>
					</table>
				</div>
				<div class="blockrow">
					<table>
						<tr><td></td><td></td><td></td><td></td></tr>
						<tr>
							<td style="width:10%"></td>
							<td><input type="text" name="txtKey1" style="width:100%; font-size:14px" autocomplete="off"></td>
							<td style="width:20%"></td>
							<td style="width:20%"></td>
							<td><input type="text" name="txtKey2" style="width:100%; font-size:14px" autocomplete="off"></td>
							<td style="width:10%"></td>
							<input type="hidden" name="random" value="<?php echo $rnd_number?>">
							<input type="hidden" name="image_ID0" value="<?php echo $image_ID[0]?>">
							<input type="hidden" name="image_ID1" value="<?php echo $image_ID[1]?>">
							<!--input type="hidden" name="userID" value="1"><This value should be modified with the real user ID-->
							
						</tr>
					</table>
				</div>	
				
				<div class="blockrow">
					<li>
						
						<object align="right">
							<input type="submit" class="button" style="font-size:14px" value="    Proceed    " >
							<input type="button" class="button" style="font-size:14px" value="    Regenerate    " onClick="window.location.reload()">
						</object>
						
						<!--?php 
							// has to be signed in to see this message since we can't deduce points from guest users
							if (isset($_SESSION['captchaWarning']) && isset($_SESSION['user']))
								echo $_SESSION['captchaWarning'];
						?-->
					</li>
				</div>
			</div>
		</div>
	</form>
</div>



<div id="footer" class="floatcontainer footer"> <!--Footer-->

	<center><a href="">Contact WebMaster</a></center>
	
</div><!-- closing div for footer -->

 </body></html>